package com.lg.partition20;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/3/27 10:01
 */
public class P1967NOIP2013_提高组_货车运输 {

    private static final int N = 10010, M = 50010, D = (int) ((int) Math.log(N) / Math.log(2)), INF = 0x3f3f3f3f;
    private static int[] parent = new int[N];
    private static Edge[] edges = new Edge[M];
    private static int[] h = new int[N], e = new int[N * 2], ne = new int[N * 2], w = new int[N * 2], depth = new int[N];
    private static int[][] f = new int[N][D + 1], minw = new int[N][D + 1];
    private static boolean[] st = new boolean[N];
    private static int[] q = new int[N];
    private static int n, m, idx = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        n = Integer.parseInt(ss[0]);
        m = Integer.parseInt(ss[1]);
        for (int i = 0; i < N; i++) parent[i] = i;
        for (int i = 0; i < m; i++) {
            ss = reader.readLine().split(" ");
            int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]), c = Integer.parseInt(ss[2]);
            edges[i] = new Edge(a, b, c);
        }
        //做一遍最大生成树并建图，在图上跑一遍lca
        kruskal();
        //lca预处理
        for (int i = 1; i <= n; i++) {
            if (st[i]) continue;
            bfs(i);
        }
        int t = Integer.parseInt(reader.readLine());
        while (t-- > 0) {
            ss = reader.readLine().split(" ");
            int a = Integer.parseInt(ss[0]), b = Integer.parseInt(ss[1]);
            //如果两个城市都没有连通，直接输出-1；如果连通说明有相同的根节点，那么最小值取决于两部分，第一部分是a到根节点的minw，第二部分是b到根节点的minw
            int ans = -1;
            if (find(a) == find(b)) ans = lca(a, b);
            writer.write(ans + "\n");
        }
        writer.flush();
    }

    private static int lca(int x, int y) {
        int res = INF;
        if (depth[x] < depth[y]) {
            int t = x;
            x = y;
            y = t;
        }
        for (int i = D; i >= 0; i--) {
            if (depth[f[x][i]] >= depth[y]) {
                res = Math.min(res, minw[x][i]);
                x = f[x][i];
            }
        }
        if (x == y) return res;
        for (int i = D; i >= 0; i--) {
            if (f[x][i] != f[y][i]) {
                res = Math.min(res, minw[x][i]);
                res = Math.min(res, minw[y][i]);
                x = f[x][i];
                y = f[y][i];
            }
        }
        res = Math.min(res, Math.min(minw[x][0], minw[y][0]));
        return res;
    }

    private static void bfs(int root) {
        int hh = 0, tt = 0;
        q[0] = root;
        depth[root] = 1;
        while (hh <= tt) {
            int u = q[hh++];
            st[u] = true;
            for (int i = h[u]; i != -1; i = ne[i]) {
                int v = e[i];
                if (depth[v] != 0) continue;
                depth[v] = depth[u] + 1;
                minw[v][0] = w[i];
                f[v][0] = u;
                for (int j = 1; j <= D; j++) {
                    int anc = f[v][j - 1];
                    f[v][j] = f[anc][j - 1];
                    minw[v][j] = INF;
                    minw[v][j] = Math.min(minw[v][j], Math.min(minw[v][j - 1], minw[anc][j - 1]));
                }
                q[++tt] = v;
            }
        }
    }

    private static void kruskal() {
        Arrays.fill(h, -1);
        Arrays.sort(edges, 0, m);
        for (int i = 0; i < m; i++) {
            int a = edges[i].u, b = edges[i].v, c = edges[i].w;
            int pa = find(a), pb = find(b);
            if (pa != pb) {
                parent[pa] = pb;
                add(a, b, c);
                add(b, a, c);
            }
        }
    }

    private static int find(int x) {
        if (x != parent[x]) parent[x] = find(parent[x]);
        return parent[x];
    }

    private static void add(int a, int b, int c) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx;
        w[idx++] = c;
    }

    private static class Edge implements Comparable<Edge> {
        private int u, v, w;

        public Edge(int u, int v, int w) {
            this.u = u;
            this.v = v;
            this.w = w;
        }

        @Override
        public int compareTo(Edge o) {
            return o.w - w;
        }
    }
}
